package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgStockLogMapper;
import com.ygqh.baby.po.YgStock;
import com.ygqh.baby.po.YgStockLog;
import com.ygqh.baby.po.YgStockLogExample;
import com.ygqh.baby.service.YgStockLogService;
import com.ygqh.baby.service.YgStockService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class YgStockLogServiceImpl implements YgStockLogService {

	@Autowired
	private YgStockLogMapper ygStockLogMapper;

	@Autowired
	private YgStockService ygStockService;

	@Override
	public List<YgStockLog> find() {
		YgStockLogExample example = new YgStockLogExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygStockLogMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgStockLogExample example = new YgStockLogExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygStockLogMapper.countByExample(example);
	}

	@Override
	public YgStockLog findById(Long id) {
		return ygStockLogMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgStockLog ygStockLog) {
		return ygStockLogMapper.insertSelective(ygStockLog);
	}

	@Override
	public int update(YgStockLog ygStockLog) {
		return ygStockLogMapper.updateByPrimaryKeySelective(ygStockLog);
	}

	@Override
	public ResultSet<YgStockLog> search(QueryInfo queryInfo, String q) {
		List<YgStockLog> list = ygStockLogMapper.selectSuper(queryInfo, q);
		int count = ygStockLogMapper.countSuper(q);
		ResultSet<YgStockLog> resultSet = new ResultSet<YgStockLog>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgStockLogExample example = new YgStockLogExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgStockLog record = new YgStockLog();
		record.setStatus(DataStatus.Delete);

		return ygStockLogMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgStockLog ygStockLog) {
		int r = 0;
		if (ygStockLog.getId() != null) {
			r = this.update(ygStockLog);
		} else {
			r = this.save(ygStockLog);
		}
		return r;
	}

	@Override
	public int addStockLogBatch(List<YgStock> stockList, String createBy, String remark) {
		if (CollectionUtils.isEmpty(stockList)) {
			return 0;
		}
		List<String> skuCodes = stockList.stream().map(s -> s.getSkuCode()).collect(Collectors.toList());

		List<YgStock> oldStock = ygStockService.findBySkuCodes(skuCodes.toArray(new String[skuCodes.size()]));

		return this.addStockLogBatch(stockList, oldStock, createBy, remark);
	}

	@Override
	public int addStock(YgStock stock, String createBy, String remark) {
		if (stock == null) {
			return 0;
		}
		List<YgStock> oldStock = ygStockService.findBySkuCodes(new String[] { stock.getSkuCode() });
		YgStockLog ygStockLog = new YgStockLog();
		ygStockLog.setSkuCode(stock.getSkuCode());
		ygStockLog.setNewAmount(stock.getAmount());
		ygStockLog.setOldAmount(oldStock.get(0).getAmount());
		ygStockLog.setCreateBy(createBy);
		ygStockLog.setCreateTime(new Date());
		ygStockLog.setRemark(remark);
		return this.save(ygStockLog);
	}

	@Override
	public int addStockLogBatch(List<YgStock> newList, List<YgStock> oldList, String createBy, String remark) {
		if (CollectionUtils.isEmpty(newList) || CollectionUtils.isEmpty(oldList)) {
			return 0;
		}
		List<YgStockLog> logs = new ArrayList<YgStockLog>();
		Date currentDate = new Date();
		for (YgStock oldS : oldList) {
			for (YgStock newS : newList) {
				if (oldS.getSkuCode().equals(newS.getSkuCode())) {
					YgStockLog log = new YgStockLog();
					log.setSkuCode(newS.getSkuCode());
					log.setNewAmount(newS.getAmount());
					log.setOldAmount(oldS.getAmount());
					log.setCreateBy(createBy);
					log.setCreateTime(currentDate);
					log.setRemark(remark);
					logs.add(log);
				}
			}
		}
		if (logs.size() == 0) {
			return 0;
		}
		return ygStockLogMapper.inserLogBatch(logs);
	}

}
